//
//  JSMStrategyViewController.m
//  TakeYouTourTogether
//
//  Created by king on 15/11/26.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMStrategyViewController.h"
#import "JSMStrategyCollectionViewCell.h"
#import "JSMStrategyElementsModel.h"
#import "JSMStrategyModel.h"
#import "JSMStrategyDetailViewController.h"
#import "ExchangeRateTableViewController.h"
#import "JSMBaiduMapViewController.h"

@interface JSMStrategyViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, strong) BaseCollectionView *myCollectionView;
@property(nonatomic, strong) UIImageView *myImageView;
@property(nonatomic, strong) UIView *errorView;
@property(nonatomic, strong) UIVisualEffectView *effectView;
@property(nonatomic, strong) NSMutableArray *dataElementsArray;

@end

@implementation JSMStrategyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataElementsArray = [JSMCollectionOfMessage getModelToPlistType:CollectionOfMessageTypeStrategyModel];
    [self createBackGroundImage];
    [self createStrategyCollection];
    [self createButtonS];
    [self requestDataFromNetWorking];
}
#pragma mark - Method 创建背景图片
- (void)createBackGroundImage {
    if (!_myImageView) {
        self.myImageView = [[UIImageView alloc] init];
        self.myImageView.frame = CGRectMake(0, SCREEN_WIDTH/3, SCREEN_WIDTH, SCREEN_HEIGHT-SCREEN_WIDTH/3);
        [self.view addSubview:self.myImageView];
        self.myImageView.image = [UIImage imageNamed:@"tutorial_background_01@2x.jpg"];
        // 毛玻璃效果
        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
        self.effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
        self.effectView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.effectView.alpha = 0.5;
        // 添加子视图
        //    [self.view addSubview:effectView];
        [self.myImageView addSubview:self.effectView];
    }
}
#pragma mark - Method
- (void)createButtonS {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:button];
    
    
    button.frame = CGRectMake(0, SCREEN_HEIGHT-180, SCREEN_WIDTH/2, 80);
    [button setTitle:@"汇率" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor whiteColor]];
    button.alpha = 0.4;
    
    button.layer.borderWidth = 2;
    button.layer.borderColor = [UIColor colorWithWhite:0.882 alpha:1.000].CGColor;
    [button setTintColor:[UIColor blackColor]];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [button addTarget:self action:@selector(ExchangeRateConvert:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(SCREEN_WIDTH/2, SCREEN_HEIGHT-180, SCREEN_WIDTH/2, 80);
    [button1 setTitle:@"百度地图" forState:UIControlStateNormal];
    [self.view addSubview:button1];
    [button1 setBackgroundColor:[UIColor whiteColor]];
    button1.alpha = 0.4;
    button1.layer.borderWidth = 2;
    button1.layer.borderColor = [UIColor colorWithWhite:0.882 alpha:1.000].CGColor;
    [button1 setTintColor:[UIColor blackColor]];
    button1.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [button1 addTarget:self action:@selector(baiduMap:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - Method 创建collectionView
- (void)createStrategyCollection {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/3, SCREEN_WIDTH/3);
    NSLog(@"%f", SCREEN_WIDTH/3);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.myCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
    [self.view addSubview:self.myCollectionView];
    [self.myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.width.equalTo(self.view.mas_width);
        make.height.mas_equalTo(SCREEN_WIDTH/3*2);
    }];
    self.myCollectionView.backgroundColor = [UIColor clearColor];
    self.myCollectionView.showsHorizontalScrollIndicator = NO;
    self.myCollectionView.showsVerticalScrollIndicator = NO;
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    [self.myCollectionView registerClass:[JSMStrategyCollectionViewCell class] forCellWithReuseIdentifier:@"aaa"];
}
#pragma mark - Method 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.dataElementsArray.count == 0) {
        return 0;
    } else {
        return self.dataElementsArray.count-1;
    }
}
#pragma mark - Method 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 0) {
        JSMStrategyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"aaa" forIndexPath:indexPath];
        JSMStrategyModel *temp = [[self.dataElementsArray[indexPath.row + 1] dataArray] firstObject];
        cell.dataModel = temp;
        return cell;
}
#pragma mark - Method 出国游点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击了");
    JSMStrategyDetailViewController *controller = [[JSMStrategyDetailViewController alloc] init];
    controller.type = indexPath.row;
    controller.path = indexPath;
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}
#pragma mark - Method requestDataFromNetWorking 网络请求数据
- (void)requestDataFromNetWorking {
    NSString *strURL = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/v3/?last_modified_since=0", nil];
    NSLog(@"%@", strURL);
    [AFNetworkingTool getUrl:strURL hudSuperView:self.view result:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSLog(@"%@", dic);//elements data
        self.dataElementsArray = [JSMStrategyElementsModel baseModelByArr:dic[@"elements"]];
        [JSMCollectionOfMessage saveModelToPlist:self.dataElementsArray type:CollectionOfMessageTypeStrategyModel];
//        [self createBackGroundImage];
//        [self createStrategyCollection];
//        [self createButtonS];
        [self.myCollectionView reloadData];
    } failure:^(NSError *error){

    }];
}

#pragma mark - Method 
- (void)ExchangeRateConvert:(UIButton *)button {
    ExchangeRateTableViewController *temp = [[ExchangeRateTableViewController alloc] init];
    temp.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:temp animated:YES];
}
#pragma mark - Method
- (void)baiduMap:(UIButton *)mapButton {
    JSMBaiduMapViewController *controller = [[JSMBaiduMapViewController alloc] init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}
#pragma mark - Method didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
